﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class CMS_Control_Vote : System.Web.UI.UserControl
{
    Usas.BLL.Cms.cms_block_common_detail cbcd = new Usas.BLL.Cms.cms_block_common_detail();
    Usas.Model.Cms.cms_block_common_detail cbcdm = new Usas.Model.Cms.cms_block_common_detail();
    Usas.BLL.Cms.cms_block cb = new Usas.BLL.Cms.cms_block();
    Usas.Common.Cms.CurrentInfo cinfo = new Usas.Common.Cms.CurrentInfo();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = cb.GetList("b_path='Vote.ascx'").Tables[0].Rows[0]["b_id"].ToString();
            string type = System.IO.Path.GetFileName(HttpContext.Current.Request.Path);

            DataSet ds = cbcd.GetList("c_b_id=" + id + " and c_type='" + type + "' and c_view=1");
            StringBuilder sbr = new StringBuilder();
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                sbr.Append("$(document).ready(function() {");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    VoteBind(Convert.ToInt32(ds.Tables[0].Rows[i]["c_id"]), ds.Tables[0].Rows[i]["c_value"].ToString(), sbr, Convert.ToInt32(ds.Tables[0].Rows[i]["c_view"]));
                }
                sbr.Append("});");
                this.Page.ClientScript.RegisterStartupScript(GetType(), "Msg", sbr.ToString(), true);
            }
            else
            {
                LitContent.Text = "<span style='color:#999;font-size:13px'>暂无新发起的投票</span><br/>";
            }
        }
    }

    private void VoteBind(int id, string c_name, StringBuilder sbr, int view)
    {
        Usas.BLL.Cms.cms_block_common_detail_option cop = new Usas.BLL.Cms.cms_block_common_detail_option();
        DataSet ds = cop.GetList("o_c_id=" + id);

        if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
        {
            LitContent.Text += "<span style=\"font-weight: bold; color: Blue\">" + c_name + "</span>";
            LitContent.Text += "<div id=\"vote" + id + "\"></div><br/>";


            sbr.Append(" $(\"#vote" + id + "\").study_vote([");

            string alert = "";
            switch (view)
            {
                case 0:
                    alert = "AlertMsg(\'投票已关闭\')";
                    break;
                case 1:
                    //alert = "ToVote(\'投票已关闭\')";
                    alert = VoteEnableBind(id.ToString());
                    break;
                case 2:
                    alert = "AlertMsg(\'投票已关闭\')";
                    break;
                default:
                    alert = "return false";
                    break;
            }

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string att = "";
                if (alert == "1")
                {
                    att = "ToVote(\'" + id + "','" + ds.Tables[0].Rows[i]["o_id"].ToString() + "\',this)";
                }
                else
                {
                    att = alert;
                }

                sbr.Append("{ \"name\": \"" + ds.Tables[0].Rows[i]["o_name"].ToString()
                    + "\", \"data\": \"" + ds.Tables[0].Rows[i]["o_value"].ToString() + "\", \"color\": \"#000\",\"art\":\"" + att + "\" }");
                if (i != ds.Tables[0].Rows.Count - 1)
                {
                    sbr.Append(",");
                }

            }

            sbr.Append("],null);");
        }
    }

    private string VoteEnableBind(string cid)
    {
        Usas.BLL.Cms.cms_vote_result cvr = new Usas.BLL.Cms.cms_vote_result();
        DataSet ds = cvr.GetList("v_c_id=" + cid + " and (v_value is not null or cast(v_value as int) != 0) and v_eid=" + cinfo.GetEmpId());
        if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
        {
            return "AlertMsg(\'您已投过，请勿重复投票\')";
        }
        else
        {
            return "1";
            //return "ToVote(\'" + cid + "','" + oid + "\')";
        }
    }

  
}
